package cn.qingyun.gis.modules.express.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.qingyun.gis.exception.CommonException;
import cn.qingyun.gis.modules.express.entity.ExpressAttachment;
import cn.qingyun.gis.modules.express.mapper.ExpressAttachmentMapper;
import cn.qingyun.gis.modules.express.service.IExpressAttachmentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 快递勘误上传附件 服务实现类
 * </p>
 *
 * @author miyunguo
 * @since 2023-07-28
 */
@Service
public class ExpressAttachmentServiceImpl extends ServiceImpl<ExpressAttachmentMapper, ExpressAttachment> implements IExpressAttachmentService {

    @Resource
    private ExpressAttachmentMapper expressAttachmentMapper;
    @Override
    @Transactional
    public void upload(List<String> pictureList,String id) {
        //上传图片不为空
        try {
            if(ObjectUtil.isEmpty(pictureList)){
                log.debug("未上传图片");
            }else{
                int count = expressAttachmentMapper.selectCount(new LambdaQueryWrapper<ExpressAttachment>().eq(ExpressAttachment::getExpressProofreadId,id));
                if(count > 0){ //附件列表中存在上次勘误上传图片
                    expressAttachmentMapper.delete(new LambdaQueryWrapper<ExpressAttachment>().eq(ExpressAttachment::getExpressProofreadId,id));
                }
                List<ExpressAttachment> expressAttachmentsList = Lists.newArrayList();
                for (String picture:pictureList) {
                    ExpressAttachment expressAttachment = new ExpressAttachment();
                    expressAttachment.setExpressProofreadId(id);
                    /*if(picture.length() >  2 * 1024 * 1024){ //字符串长度超出4m
                        String pictureNew = ImageysUtils.compressToBase64(picture);
                        expressAttachment.setContent(pictureNew);
                    }else{
                        expressAttachment.setContent(picture);
                    }*/
                    expressAttachment.setContent(picture);
                    expressAttachment.setUploadTime(DateUtil.date());
                    expressAttachmentsList.add(expressAttachment);
                }
                this.saveBatch(expressAttachmentsList);
            }
        } catch (Exception e) {
            throw new CommonException("图片上传失败");
        }
    }

    @Override
    public List<ExpressAttachment> show(String id) {
        if(CharSequenceUtil.isEmpty(id)){
           throw new CommonException("传入id不能为空");
        }
        //返回图片信息
        return expressAttachmentMapper.selectList(new LambdaQueryWrapper<ExpressAttachment>().eq(ExpressAttachment::getExpressProofreadId,id));
    }
}
